package com.sky.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOSSUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 通用控制器
 */
@RestController
@RequestMapping(value = "admin/common")
@Tag(name = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiResponses(
        @ApiResponse(responseCode = "200",description = "成功上传文件")
    )
    @Operation(summary = "文件上传",description = "文件上传")
    public Result<String> upload(MultipartFile file) throws IOException, ClientException {
        String url=null;
        try {
            log.info("文件上传：{}",file.getOriginalFilename());
//        aliOSSUtils.upload(file.getBytes(),fileName)
            url=aliOSSUtils.upload(file);
        }catch (Exception e){
            log.info("文件上传失败：{}", MessageConstant.UPLOAD_FAILED);
        }
        return Result.success(url);
    }
}
